﻿using Quartz;
using Quartz.Impl;
using Quartz.Spi;

namespace QYmall.Core.API.Quartz
{
    /// <summary>
    /// Quartz服务
    /// </summary>
    public static class QuartzJobSetup
    {
        /// <summary>
        /// 添加Quartz服务
        /// </summary>
        public static void AddJobSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            //添加Quartz服务
            services.AddSingleton<IJobFactory, QuartzJobFactory>();
            services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();

            //取消收款单（10分钟）
            services.AddSingleton<OrderCancelJobs>();
            services.AddSingleton(
                new JobSchedule(jobType: typeof(OrderCancelJobs), cronExpression: "0 */10 * * * ?")
            );
            //确认订单（每天凌晨1点）
            services.AddSingleton<OrderCompleteJobs>();
            services.AddSingleton(
                new JobSchedule(jobType: typeof(OrderCompleteJobs), cronExpression: "0 0 1 * * ?")
            );
            //结算订单（每天凌晨3点）
            services.AddSingleton<OrderSettleJobs>();
            services.AddSingleton(
                new JobSchedule(jobType: typeof(OrderSettleJobs), cronExpression: "0 0 3 * * ?")
            );

            services.AddHostedService<QuartzHostedService>();
        }
    }
}
